package com.hghivln.blog.controller;


import com.hghivln.blog.common.lang.Result;
import com.hghivln.blog.entity.Link;
import com.hghivln.blog.service.LinkService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 友情链接表 前端控制器
 * </p>
 *
 * @author hghivln
 * @since 2021-09-06
 */
@RestController
@RequestMapping("/link")
public class LinkController {
    
    @Autowired
    private LinkService linkService;

    /**
     * 保存
     */
    @PostMapping("save")
    public Result<?> addLink(@RequestBody Link link) {
        try {
            linkService.save(link);
            return new Result<>("保存成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result<>("保存失败");
        }
    }

    /**
     * 更新
     */
    @PutMapping("update")
    public Result<?> update(@RequestBody Link link) {
        try {
            linkService.updateById(link);
            return new Result<>("修改成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result<>("修改失败");
        }
    }

    /**
     * 删除
     */
    @DeleteMapping( "/delete/{id}")
    public Result<?> deleteById(@PathVariable Long id) {
        try {
            linkService.deleteById(id);
            return new Result<>("删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result<>("删除失败");
        }
    }

    /**
     * 根据ID查询
     */
    @GetMapping("/get/{id}")
    public Result<Link> get(@PathVariable Long id) {
        Link link = linkService.getById(id);
        return new Result<>(link);
    }

    /**
     * 列表查询
     */
    @GetMapping("list")
    public Result<List<Link>> get() {
        List<Link> list = linkService.getAllList();
        return new Result<>(list);
    }
}
